#include "cards.h"

Cards::Cards()
{	
	for (int i = 0; i < 52; i++) {
		Clip[i].x = 0;
		Clip[i].y = 0;
		Clip[i].h = cardHeight;
		Clip[i].w = cardWidth;
		flag[i] = false;
	}
	
	//TREF
	for (int i = 0; i < 13; i++) {
		Clip[i].x = cardWidth * i;
		Clip[i].y = 0;
		points[i] = calculate_points(i);
	}
	
	//KARO
	for (int i = 0, j = 13; i < 13; i++, j++) {
		Clip[j].x = cardWidth * i;
		Clip[j].y = cardHeight;
		points[j] = calculate_points(i);
	}
	
	//HERC
	for (int i = 0, j = 26; i < 13; i++, j++) {
		Clip[j].x = cardWidth * i;
		Clip[j].y = cardHeight * 2;
		points[j] = calculate_points(i);
	}
	
	//PIK
	for (int i = 0, j = 39; i < 13; i++, j++) {
		Clip[j].x = cardWidth * i;
		Clip[j].y = cardHeight * 3;
		points[j] = calculate_points(i);
	}
	//Back side of the card
	Clip[52].x = cardWidth * 2;
	Clip[52].y = cardHeight * 4;
	Clip[52].h = cardHeight;
	Clip[52].w = cardWidth;
}

unsigned int Cards::calculate_points(int i)
{
	i < 10 ? ++i : i = 10;
	return i;
}

bool Cards::GetFlag(int i)
{
	return flag[i];
}

unsigned int Cards::CalculateCardNb()	//TEST PASSED!!!
{
	srand(time(0));
	int value, count = 0;
	while (true) {
		value = rand() % 52;
		
		//If computer can't calculate
		//card we are gonna found it manually.
		if (count > 10) {
			for (int i = 0; i < 52; i++) {
				if (flag[i] == false) {
					flag[i] = true;
					return value;
				}
			}
			exit(0);
		} else if (flag[value] == true) {
			count++;
			continue;
		} else if (flag[value] == false) {
			flag[value] = true;
			return value;
		} else
			exit(0);
	}
}

unsigned int Cards::GetPoints(int i)
{
	return points[i];
}

SDL_Rect *Cards::GetClipAddress(int ClipNB)
{
	return &Clip[ClipNB];
}

/*
	Help function for CalculateCardValue
	Check if card is AS or not.
*/
bool isAs(int card, int *asnb)
{
	if (card == asnb[0])
		return true;
	else if (card == asnb[1])
		return true;
	else if (card == asnb[2])
		return true;
	else if (card == asnb[3])
		return true;
	else
		return false;
}

/*
	Algorithm for calculating card value. Should pass two arguments:
	1. is number of the card 0-51
	2. is current user score, because this algorithm should calculate
	if value for AS is 1 or 11
	TEST : tested for various numbers; hearts have problems with
	calculating, so I changed second operand in rValue = cardNB - 26;
	and put rValue = cardNB - 27;
*/
int Cards::CalculateCardValue(int cardNB, int currentScore)
{
	int specialValues[4] = {1,14,27,40};	//have card numbers for 
						//AS-es
	int rValue;
	cardNB = cardNB+1;
	if (isAs(cardNB, specialValues) == false) {	//card is not AS
		if (cardNB < 14) {	//card is in the first row
			if (cardNB > 10)
				return 10;
			return cardNB;
		} else if (cardNB > 14 && cardNB < 27) {//card is in the
							//second row
			rValue = cardNB - 13;
			if (rValue > 10)
				return 10;
			return rValue;
		} else if (cardNB > 27 && cardNB < 40) {
			rValue = cardNB - 27;
			if (rValue >= 10)	//why is only here >=
						//I don't know
				return 10;
			return rValue+1;
		} else if (cardNB > 40) {
			rValue = cardNB - 39;
			if (rValue > 10)
				return 10;
			return rValue;
		}
	} else {	//is AS
		if ((currentScore + 11) > 21)
			return 1;
		else
			return 11;
	}
}

void Cards::ResetAll()
{
	for (int i = 0; i < 52; i++)
		flag[i] = false;
}